﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSSQL_Connection;
using IPL;
using SHARED;
using XML;
using System.Xml.Linq;

namespace GUI_Test
{
    public partial class formMain : Form
    {
       // List<SHARED.IDb> dbs = new List<SHARED.IDb>();
        SHARED.IDb db = null;
        public formMain()
        {
            InitializeComponent();
            textBoxSQL.Text = "SELECT ZIPCODE, <school, SCHOOL_GK > FROM WC_SCHOOL LIMIT 10";
        }

        
        private void Convert(object sender, EventArgs e)
        {
            try
            {
                textBoxXML.Text = string.Empty;

                if (validateUserInput() != string.Empty)
                {
                    MessageBox.Show(validateUserInput(), "Error: Invalid Entry");
                    return;
                }
                //if (db == null)
                //{
                //    MessageBox.Show("Please connect to a database.", "error");
                //    return;
                //}

                Lexer lexer = new Lexer(textBoxSQL.Text.Trim());
                Parser parser = new Parser(lexer);
                ParserOutput pOutput = parser.Parse();

                BuildTree buildtree = new BuildTree();

               // MasterElementList list = buildtree.BuildTreeFromDatabase(db, pOutput.SQL, pOutput.ElementList, pOutput.RootElement);

                string schemaText = buildtree.GetSchema(pOutput.ElementList);
                if (schemaText == string.Empty)
                {
                    //could not generate schema
                    MessageBox.Show("", "error");
                    return;
                }
                textBoxXML.Text = schemaText;
               
                MessageBox.Show("schema generated");
            }
            catch (Exception ex)
            {

                MessageBox.Show("unknown error: " + ex.Message, "Exception");
                return;
            }
           
        }

        private string validateUserInput()
        {
            
            string errorMessage = string.Empty;

            if (textBoxSQL.Text.Trim() == string.Empty)
            {
                errorMessage += "Please enter your SQL statement.";
                return errorMessage;               
            }
            if (textBoxSQL.Text.Contains("SELECT") == false)
            {
                errorMessage += "The SQL statement is missing keyword SELECT or has an invalid terminator.";
                return errorMessage;
            }
            //if (textBoxSQL.Text.Contains(";") == false)
            //{
            //    errorMessage += "The SQL statement is missing keyword SELECT or has an invalid terminator.";
            //    return errorMessage;
            //}
            if (textBoxSQL.Text.Contains("FROM") == false)
            {
                errorMessage += "The SQL statement is missing keyword SELECT has an invalid terminator.";
                return errorMessage;
            }
            return errorMessage;
        }


        private void menuQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (textBoxSQL.SelectedText != "")
            {
            textBoxSQL.Cut();
            }
        }

        private void menuCopy_Click(object sender, EventArgs e)
        {
            if (textBoxSQL.SelectedText != "")
            {
                textBoxSQL.Copy();
            }
        }

        private void menuPaste_Click(object sender, EventArgs e)
        {
            textBoxSQL.Paste();
        }

        private void menuUndo_Click(object sender, EventArgs e)
        {
            if (textBoxSQL.CanUndo == true)
            {
                textBoxSQL.Undo();
            }
        }


        private void menuOracle_Click(object sender, EventArgs e)
        {
            //SubFormOracleConnection formChild;
            using (SubFormOracleConnection formChild = new SubFormOracleConnection())
            {
                if (formChild.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    db = new SHARED.Oracle(formChild.textBoxOracleServerName.Text, int.Parse(formChild.textBoxOracleDBName.Text),
                        formChild.textBoxOracleServiceId.Text, formChild.textBoxOracleUserId.Text, formChild.textBoxOraclePassword.Text);
                    formChild.Dispose();
                }
            }


        }

        private void btn_CreateXml_Click(object sender, EventArgs e)
        {
            try
            {
                if (validateUserInput() != string.Empty)
                {
                    MessageBox.Show(validateUserInput(), "Error: Invalid Entry");
                    return;
                }
                if (db == null)
                {
                    MessageBox.Show("Please connect to a database.", "error");
                    return;
                }

                Lexer lexer = new Lexer(textBoxSQL.Text.Trim());
                Parser parser = new Parser(lexer);
                ParserOutput pOutput = parser.Parse();

                BuildTree buildtree = new BuildTree();

                MasterElementList list = buildtree.BuildTreeFromDatabase(db, pOutput.SQL, pOutput.ElementList, pOutput.RootElement);

                XElement result = null;
                if (pOutput.SearchType == 2)
                {
                    result = buildtree.GenerateXMLUsingCompressionTechnique(list);
                }
                else if (pOutput.SearchType == 1)
                {
                    result = buildtree.GenerateXMLUsingGroupingTechnique(list);
                }
                else
                {
                    result = buildtree.GenerateBasicXML(list);
                }

                Output o = new Output("output");

                //based on used selection
                //MessageBox.Show("Please choose data format.");

                o.toDTD(pOutput.ElementList, result);
                o.toXSD(result);

                MessageBox.Show("Your file was successfully created.");
            }
            catch (Exception ex)
            {

                MessageBox.Show("unknown error: " + ex.Message, "Exception");
                return;
            }

            
        }

        private void menuMSSQL_Click(object sender, EventArgs e)
        {
            SubFormMSSQLConnection formChilid;
            formChilid = new SubFormMSSQLConnection();
            
            
            if (formChilid.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                db = new SHARED.MSSQL(formChilid.textBoxMSSQLServerName.Text, formChilid.textBoxMSSQLDBName.Text,
                    formChilid.textBoxMSSQLUserId.Text, formChilid.textBoxMSSQLPassword.Text);
                formChilid.Dispose();
            }

            
        }

        private void menuMYSQL_Click(object sender, EventArgs e)
        {
            SubFormMYSQLConnection formChild;
            formChild = new SubFormMYSQLConnection();
            if (formChild.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                db = new SHARED.MYSQL(formChild.textBoxMYSQLServerName.Text, formChild.textBoxMYSQLDBName.Text,
                    formChild.textBoxMYSQLUserId.Text, formChild.textBoxMYSQLPassword.Text);

                formChild.Dispose();
            }
        }

        private void menuAccess_Click(object sender, EventArgs e)
        {
            SubFormAccessConnection formChild;
            formChild = new SubFormAccessConnection();
            if (formChild.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               db = new SHARED.Access(formChild.textBoxAccessFilename.Text, formChild.textBoxAccessPassword.Text);
            }
        }

        private void textboxSQL_TextChanged(object sender, EventArgs e)
        {
            //textBoxSQL.CharacterCasing = CharacterCasing.Upper;
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {

        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void formMain_Load(object sender, EventArgs e)
        {

        }

        private void xSDToolStripMenuItem_Click(object sender, EventArgs e)
        {
          
        }

        private void createXMLFileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBoxSQL.Text != "")
            {
                textBoxSQL.SelectAll();

            }
        }

        
    }
}
